{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/workshops/AmericaView_2023.ipynb)\n",
    "\n",
    "**Interactive Cloud Computing with Google Earth Engine and Geemap**\n",
    "\n",
    "**Introduction:** This is a notebook prepared for the workshop at the [AmericaView](https://americaview.org/) Annual Conference at Lafayette, Louisiana on March 13, 2023.\n",
    "\n",
    "**Overview:** Google Earth Engine ([GEE](https://earthengine.google.com/)) is a cloud computing platform with a multi-petabyte catalog of satellite imagery and geospatial datasets. It enables scientists, researchers, and developers to analyze and visualize changes on the Earth’s surface. The [geemap](https://geemap.org/) Python package provides GEE users with an intuitive interface to manipulate, analyze, and visualize geospatial big data interactively in a Jupyter-based environment. The topics will be covered in this workshop include: (1) introducing geemap and the Earth Engine Python API; (2) creating interactive maps; (3) searching GEE data catalog; (4) visualizing GEE datasets; (5) analyzing GEE datasets, and (6) exporting GEE datasets. More information about the geemap Python package can be found at <https://geemap.org>.\n",
    "\n",
    "**Requirement:** Please [sign up](https://earthengine.google.com/signup/) for a Google Earth Engine account if you don’t have one yet. No software installation is needed. You just need a browser with Internet access for this workshop."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "## Why Earth Engine Python API and geemap?\n",
    "\n",
    "Check out the slides [here](https://docs.google.com/presentation/d/1VpfKNG5aubcNLIu89MHMxwwiXBJea4BBYnWukRV_XHs/edit?pli=1#slide=id.gb3de725ea9_0_788).\n",
    "\n",
    "## Install packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Authenticate Earth Engine\n",
    "\n",
    "You will need to create a [Google Cloud Project](https://console.cloud.google.com/projectcreate) and enable the [Earth Engine API](https://console.cloud.google.com/apis/api/earthengine.googleapis.com) for the project. You can find detailed instructions [here](https://book.geemap.org/chapters/01_introduction.html#earth-engine-authentication)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Create interactive maps\n",
    "\n",
    "Let's create an interactive map using the `ipyleaflet` plotting backend. The [`geemap.Map`](https://geemap.org/geemap/#geemap.geemap.Map) class inherits from the [`ipyleaflet.Map`](https://ipyleaflet.readthedocs.io/en/latest/map_and_basemaps/map.html) class. Therefore, you can use the same syntax to create an interactive map as you would with `ipyleaflet.Map`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "To display it in a Jupyter notebook, simply ask for the object representation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "To customize the map, you can specify various keyword arguments, such as `center` ([lat, lon]), `zoom`, `width`, and `height`. The default `width` is `100%`, which takes up the entire cell width of the Jupyter notebook. The `height` argument accepts a number or a string. If a number is provided, it represents the height of the map in pixels. If a string is provided, the string must be in the format of a number followed by `px`, e.g., `600px`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4, height=600)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "To hide a control, set `control_name` to `False`, e.g., `draw_ctrl=False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(data_ctrl=False, toolbar_ctrl=False, draw_ctrl=False)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "## Adding basemaps\n",
    "\n",
    "There are several ways to add basemaps to a map. You can specify the basemap to use in the `basemap` keyword argument when creating the map. Alternatively, you can add basemap layers to the map using the `add_basemap` method. Geemap has hundreds of built-in basemaps available that can be easily added to the map with only one line of code.\n",
    "\n",
    "### Built-in basemaps\n",
    "\n",
    "Create a map by specifying the basemap to use as follows. For example, the `HYBRID` basemap represents the Google Satellite Hybrid basemap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(basemap=\"HYBRID\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "You can add as many basemaps as you like to the map. For example, the following code adds the `OpenTopoMap` basemap to the map above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_basemap(\"OpenTopoMap\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "Print out the first 10 basemaps:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "basemaps = list(geemap.basemaps.keys())\n",
    "len(geemap.basemaps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "basemaps[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "### XYZ tiles\n",
    "\n",
    "You can also add XYZ tile layers to the map using the `Map.add_tile_layer()` method. For example, the following code creates an interactive map and adds the Google Terrain basemap to it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_tile_layer(\n",
    "    url=\"https://mt1.google.com/vt/lyrs=p&x={x}&y={y}&z={z}\",\n",
    "    name=\"Google Terrain\",\n",
    "    attribution=\"Google\",\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "### WMS tiles\n",
    "\n",
    "Similarly, you can add WMS tile layers to the map using the `Map.add_wms_layer()` method. For example, the following code creates an interactive map and adds the National Land Cover Database (NLCD) 2019 basemap to it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "url = \"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?\"\n",
    "Map.add_wms_layer(\n",
    "    url=url,\n",
    "    layers=\"NLCD_2019_Land_Cover_L48\",\n",
    "    name=\"NLCD 2019\",\n",
    "    format=\"image/png\",\n",
    "    attribution=\"MRLC\",\n",
    "    transparent=True,\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26",
   "metadata": {},
   "source": [
    "## Earth Engine data types\n",
    "\n",
    "Earth Engine objects are server-side objects rather than client-side objects, which means that they are not stored locally on your computer. Similar to video streaming services (e.g., YouTube, Netflix, and Hulu), which store videos/movies on their servers, Earth Engine data are stored on the Earth Engine servers. We can stream geospatial data from Earth Engine on-the-fly without having to download the data just like we can watch videos from streaming services using a web browser without having to download the entire video to your computer.\n",
    "\n",
    "-   **Image**: the fundamental raster data type in Earth Engine.\n",
    "-   **ImageCollection**: a stack or time-series of images.\n",
    "-   **Geometry**: the fundamental vector data type in Earth Engine.\n",
    "-   **Feature**: a Geometry with attributes.\n",
    "-   **FeatureCollection**: a set of features.\n",
    "\n",
    "### Image\n",
    "\n",
    "Raster data in Earth Engine are represented as **Image** objects. Images are composed of one or more bands and each band has its own name, data type, scale, mask and projection. Each image has metadata stored as a set of properties.\n",
    "\n",
    "#### Loading Earth Engine images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28",
   "metadata": {},
   "source": [
    "#### Visualizing Earth Engine images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[21.79, 70.87], zoom=3)\n",
    "image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 6000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "Map.addLayer(image, vis_params, \"SRTM\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30",
   "metadata": {},
   "source": [
    "### ImageCollection\n",
    "\n",
    "An `ImageCollection` is a stack or sequence of images. An `ImageCollection` can be loaded by passing an Earth Engine asset ID into the `ImageCollection` constructor. You can find `ImageCollection` IDs in the [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets).\n",
    "\n",
    "#### Loading image collections\n",
    "\n",
    "For example, to load the image collection of the [Sentinel-2 surface reflectance](https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2_SR):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = ee.ImageCollection(\"COPERNICUS/S2_SR\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32",
   "metadata": {},
   "source": [
    "#### Visualizing image collections\n",
    "\n",
    "To visualize an Earth Engine **ImageCollection**, we need to convert an **ImageCollection** to an **Image** by compositing all the images in the collection to a single image representing, for example, the min, max, median, mean or standard deviation of the images. For example, to create a median value image from a collection, use the `collection.median()` method. Let's create a median image from the Sentinel-2 surface reflectance collection:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "collection = ee.ImageCollection(\"COPERNICUS/S2_SR\")\n",
    "image = collection.median()\n",
    "\n",
    "vis = {\n",
    "    \"min\": 0.0,\n",
    "    \"max\": 3000,\n",
    "    \"bands\": [\"B4\", \"B3\", \"B2\"],\n",
    "}\n",
    "\n",
    "Map.setCenter(83.277, 17.7009, 12)\n",
    "Map.addLayer(image, vis, \"Sentinel-2\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34",
   "metadata": {},
   "source": [
    "#### Filtering image collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "collection = (\n",
    "    ee.ImageCollection(\"COPERNICUS/S2_SR\")\n",
    "    .filterDate(\"2021-01-01\", \"2022-01-01\")\n",
    "    .filter(ee.Filter.lt(\"CLOUDY_PIXEL_PERCENTAGE\", 5))\n",
    ")\n",
    "image = collection.median()\n",
    "\n",
    "vis = {\n",
    "    \"min\": 0.0,\n",
    "    \"max\": 3000,\n",
    "    \"bands\": [\"B4\", \"B3\", \"B2\"],\n",
    "}\n",
    "\n",
    "Map.setCenter(83.277, 17.7009, 12)\n",
    "Map.addLayer(image, vis, \"Sentinel-2\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36",
   "metadata": {},
   "source": [
    "### FeatureCollection\n",
    "\n",
    "A **FeatureCollection** is a collection of Features. A FeatureCollection is analogous to a GeoJSON FeatureCollection object, i.e., a collection of features with associated properties/attributes. Data contained in a shapefile can be represented as a FeatureCollection.\n",
    "\n",
    "#### Loading feature collections\n",
    "\n",
    "The [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets) hosts a variety of vector datasets (e.g,, US Census data, country boundaries, and more) as feature collections. You can find feature collection IDs by searching the data catalog. For example, to load the [TIGER roads data](https://developers.google.com/earth-engine/datasets/catalog/TIGER_2016_Roads) by the U.S. Census Bureau:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "fc = ee.FeatureCollection(\"TIGER/2016/Roads\")\n",
    "Map.setCenter(-73.9596, 40.7688, 12)\n",
    "Map.addLayer(fc, {}, \"Census roads\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38",
   "metadata": {},
   "source": [
    "#### Filtering feature collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "fc = states.filter(ee.Filter.eq(\"NAME\", \"Louisiana\"))\n",
    "Map.addLayer(fc, {}, \"Louisiana\")\n",
    "Map.centerObject(fc)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40",
   "metadata": {},
   "outputs": [],
   "source": [
    "feat = fc.first()\n",
    "feat.toDictionary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "fc = states.filter(ee.Filter.inList(\"NAME\", [\"California\", \"Oregon\", \"Washington\"]))\n",
    "Map.addLayer(fc, {}, \"West Coast\")\n",
    "Map.centerObject(fc)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42",
   "metadata": {},
   "outputs": [],
   "source": [
    "region = Map.user_roi\n",
    "if region is None:\n",
    "    region = ee.Geometry.BBox(-88.40, 29.88, -77.90, 35.39)\n",
    "\n",
    "fc = ee.FeatureCollection(\"TIGER/2018/States\").filterBounds(region)\n",
    "Map.addLayer(fc, {}, \"Southeastern U.S.\")\n",
    "Map.centerObject(fc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43",
   "metadata": {},
   "source": [
    "#### Visualizing feature collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "Map.addLayer(states, {}, \"US States\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "style = {\"color\": \"0000ffff\", \"width\": 2, \"lineType\": \"solid\", \"fillColor\": \"FF000080\"}\n",
    "Map.addLayer(states.style(**style), {}, \"US States\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "vis_params = {\n",
    "    \"color\": \"000000\",\n",
    "    \"colorOpacity\": 1,\n",
    "    \"pointSize\": 3,\n",
    "    \"pointShape\": \"circle\",\n",
    "    \"width\": 2,\n",
    "    \"lineType\": \"solid\",\n",
    "    \"fillColorOpacity\": 0.66,\n",
    "}\n",
    "palette = [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"]\n",
    "Map.add_styled_vector(\n",
    "    states, column=\"NAME\", palette=palette, layer_name=\"Styled vector\", **vis_params\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47",
   "metadata": {},
   "source": [
    "## Earth Engine Data Catalog\n",
    "\n",
    "The [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets) hosts a variety of geospatial datasets. As of March 2023, the catalog contains over [1,000 datasets](https://github.com/samapriya/Earth-Engine-Datasets-List) with a total size of over 40 petabytes. Some notable datasets include: Landsat, Sentinel, MODIS, NAIP, etc. For a complete list of datasets in CSV or JSON formats, see the [Earth Engine Datasets List](https://github.com/giswqs/Earth-Engine-Catalog/blob/master/gee_catalog.tsv).\n",
    "\n",
    "### Searching for datasets\n",
    "\n",
    "The [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets/catalog) is searchable. You can search datasets by name, keyword, or tag. For example, enter \"elevation\" in the search box will filter the catalog to show only datasets containing \"elevation\" in their name, description, or tags. 52 datasets are returned for this search query. Scroll down the list to find the [NASA SRTM Digital Elevation 30m](https://developers.google.com/earth-engine/datasets/catalog/USGS_SRTMGL1_003#description) dataset. On each dataset page, you can find the following information, including Dataset Availability, Dataset Provider, Earth Engine Snippet, Tags, Description, Code Example, and more (see {numref}`ch03_gee_srtm`). One important piece of information is the Image/ImageCollection/FeatureCollection ID of each dataset, which is essential for accessing the dataset through the Earth Engine JavaScript or Python APIs.\n",
    "\n",
    "![](https://i.imgur.com/B3rf4QN.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_xyz = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "Map.addLayer(dataset_xyz, {}, \"USGS/SRTMGL1_003\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "Map.addLayer(dem, vis_params, \"SRTM DEM\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50",
   "metadata": {},
   "source": [
    "### Using the datasets module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geemap.datasets import DATA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "dataset = ee.Image(DATA.USGS_GAP_CONUS_2011)\n",
    "Map.addLayer(dataset, {}, \"GAP CONUS\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geemap.datasets import get_metadata\n",
    "\n",
    "get_metadata(DATA.USGS_GAP_CONUS_2011)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54",
   "metadata": {},
   "source": [
    "## Using the inspector tool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=(40, -100), zoom=4)\n",
    "\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\").select(\n",
    "    [\"B1\", \"B2\", \"B3\", \"B4\", \"B5\", \"B7\"]\n",
    ")\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "\n",
    "Map.addLayer(dem, vis_params, \"SRTM DEM\")\n",
    "Map.addLayer(\n",
    "    landsat7,\n",
    "    {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2.0},\n",
    "    \"Landsat 7\",\n",
    ")\n",
    "Map.addLayer(states, {}, \"US States\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56",
   "metadata": {},
   "source": [
    "## Converting JavaScript to Python\n",
    "\n",
    "Find some Earth Engine JavaScript code that you want to convert to Python. For example, you can grab some sample code from the [Earth Engine Documentation](https://developers.google.com/earth-engine/guides/image_visualization)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load an image.\n",
    "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\")\n",
    "\n",
    "# Define the visualization parameters.\n",
    "vizParams = {\"bands\": [\"B5\", \"B4\", \"B3\"], \"min\": 0, \"max\": 0.5, \"gamma\": [0.95, 1.1, 1]}\n",
    "\n",
    "# Center the map and display the image.\n",
    "Map.setCenter(-122.1899, 37.5010, 10)\n",
    "# San Francisco Bay\n",
    "Map.addLayer(image, vizParams, \"False color composite\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59",
   "metadata": {},
   "source": [
    "## Using the plotting tool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\").select(\n",
    "    [\"B1\", \"B2\", \"B3\", \"B4\", \"B5\", \"B7\"]\n",
    ")\n",
    "\n",
    "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n",
    "Map.addLayer(landsat7, landsat_vis, \"Landsat\")\n",
    "\n",
    "hyperion = ee.ImageCollection(\"EO1/HYPERION\").filter(\n",
    "    ee.Filter.date(\"2016-01-01\", \"2017-03-01\")\n",
    ")\n",
    "\n",
    "hyperion_vis = {\n",
    "    \"min\": 1000.0,\n",
    "    \"max\": 14000.0,\n",
    "    \"gamma\": 2.5,\n",
    "}\n",
    "Map.addLayer(hyperion, hyperion_vis, \"Hyperion\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.set_plot_options(add_marker_cluster=True, overlay=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62",
   "metadata": {},
   "source": [
    "## Creating legends\n",
    "\n",
    "### Built-in legends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63",
   "metadata": {},
   "outputs": [],
   "source": [
    "legends = geemap.builtin_legends\n",
    "for legend in legends:\n",
    "    print(legend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_legend(builtin_legend=\"NLCD\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "\n",
    "nlcd = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n",
    "landcover = nlcd.select(\"landcover\")\n",
    "\n",
    "Map.addLayer(landcover, {}, \"NLCD Land Cover 2019\")\n",
    "Map.add_legend(\n",
    "    title=\"NLCD Land Cover Classification\", builtin_legend=\"NLCD\", height=\"465px\"\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66",
   "metadata": {},
   "source": [
    "### Custom legends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(add_google_map=False)\n",
    "\n",
    "labels = [\"One\", \"Two\", \"Three\", \"Four\", \"etc\"]\n",
    "\n",
    "# colors can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n",
    "colors = [\"#8DD3C7\", \"#FFFFB3\", \"#BEBADA\", \"#FB8072\", \"#80B1D3\"]\n",
    "# legend_colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68 123)]\n",
    "\n",
    "Map.add_legend(labels=labels, colors=colors, position=\"bottomright\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "legend_dict = {\n",
    "    \"11 Open Water\": \"466b9f\",\n",
    "    \"12 Perennial Ice/Snow\": \"d1def8\",\n",
    "    \"21 Developed, Open Space\": \"dec5c5\",\n",
    "    \"22 Developed, Low Intensity\": \"d99282\",\n",
    "    \"23 Developed, Medium Intensity\": \"eb0000\",\n",
    "    \"24 Developed High Intensity\": \"ab0000\",\n",
    "    \"31 Barren Land (Rock/Sand/Clay)\": \"b3ac9f\",\n",
    "    \"41 Deciduous Forest\": \"68ab5f\",\n",
    "    \"42 Evergreen Forest\": \"1c5f2c\",\n",
    "    \"43 Mixed Forest\": \"b5c58f\",\n",
    "    \"51 Dwarf Scrub\": \"af963c\",\n",
    "    \"52 Shrub/Scrub\": \"ccb879\",\n",
    "    \"71 Grassland/Herbaceous\": \"dfdfc2\",\n",
    "    \"72 Sedge/Herbaceous\": \"d1d182\",\n",
    "    \"73 Lichens\": \"a3cc51\",\n",
    "    \"74 Moss\": \"82ba9e\",\n",
    "    \"81 Pasture/Hay\": \"dcd939\",\n",
    "    \"82 Cultivated Crops\": \"ab6c28\",\n",
    "    \"90 Woody Wetlands\": \"b8d9eb\",\n",
    "    \"95 Emergent Herbaceous Wetlands\": \"6c9fb8\",\n",
    "}\n",
    "\n",
    "nlcd = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n",
    "landcover = nlcd.select(\"landcover\")\n",
    "\n",
    "Map.addLayer(landcover, {}, \"NLCD Land Cover 2019\")\n",
    "Map.add_legend(title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69",
   "metadata": {},
   "source": [
    "## Creating color bars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "\n",
    "Map.addLayer(dem, vis_params, \"SRTM DEM\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_colorbar(vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_colorbar(\n",
    "    vis_params, label=\"Elevation (m)\", layer_name=\"SRTM DEM\", orientation=\"vertical\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_colorbar(\n",
    "    vis_params,\n",
    "    label=\"Elevation (m)\",\n",
    "    layer_name=\"SRTM DEM\",\n",
    "    orientation=\"vertical\",\n",
    "    transparent_bg=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74",
   "metadata": {},
   "source": [
    "## Split-panel maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.split_map(left_layer=\"HYBRID\", right_layer=\"TERRAIN\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=(40, -100), zoom=4, height=600)\n",
    "\n",
    "nlcd_2001 = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2001\").select(\"landcover\")\n",
    "nlcd_2019 = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select(\"landcover\")\n",
    "\n",
    "left_layer = geemap.ee_tile_layer(nlcd_2001, {}, \"NLCD 2001\")\n",
    "right_layer = geemap.ee_tile_layer(nlcd_2019, {}, \"NLCD 2019\")\n",
    "\n",
    "Map.split_map(left_layer, right_layer)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77",
   "metadata": {},
   "source": [
    "## Linked maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = (\n",
    "    ee.ImageCollection(\"COPERNICUS/S2\")\n",
    "    .filterDate(\"2018-09-01\", \"2018-09-30\")\n",
    "    .map(lambda img: img.divide(10000))\n",
    "    .median()\n",
    ")\n",
    "\n",
    "vis_params = [\n",
    "    {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n",
    "    {\"bands\": [\"B8\", \"B11\", \"B4\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n",
    "    {\"bands\": [\"B8\", \"B4\", \"B3\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n",
    "    {\"bands\": [\"B12\", \"B12\", \"B4\"], \"min\": 0, \"max\": 0.3, \"gamma\": 1.3},\n",
    "]\n",
    "\n",
    "labels = [\n",
    "    \"Natural Color (B4/B3/B2)\",\n",
    "    \"Land/Water (B8/B11/B4)\",\n",
    "    \"Color Infrared (B8/B4/B3)\",\n",
    "    \"Vegetation (B12/B11/B4)\",\n",
    "]\n",
    "\n",
    "geemap.linked_maps(\n",
    "    rows=2,\n",
    "    cols=2,\n",
    "    height=\"300px\",\n",
    "    center=[38.4151, 21.2712],\n",
    "    zoom=12,\n",
    "    ee_objects=[image],\n",
    "    vis_params=vis_params,\n",
    "    labels=labels,\n",
    "    label_position=\"topright\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79",
   "metadata": {},
   "source": [
    "## Timeseries inspector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "collection = ee.ImageCollection(\"USGS/NLCD_RELEASES/2019_REL/NLCD\").select(\"landcover\")\n",
    "vis_params = {\"bands\": [\"landcover\"]}\n",
    "years = collection.aggregate_array(\"system:index\").getInfo()\n",
    "years"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.ts_inspector(\n",
    "    left_ts=collection,\n",
    "    right_ts=collection,\n",
    "    left_names=years,\n",
    "    right_names=years,\n",
    "    left_vis=vis_params,\n",
    "    right_vis=vis_params,\n",
    "    width=\"80px\",\n",
    ")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82",
   "metadata": {},
   "source": [
    "## Time slider\n",
    "\n",
    "### Visualizing vegetation data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "collection = (\n",
    "    ee.ImageCollection(\"MODIS/MCD43A4_006_NDVI\")\n",
    "    .filter(ee.Filter.date(\"2018-06-01\", \"2018-07-01\"))\n",
    "    .select(\"NDVI\")\n",
    ")\n",
    "vis_params = {\n",
    "    \"min\": 0.0,\n",
    "    \"max\": 1.0,\n",
    "    \"palette\": \"ndvi\",\n",
    "}\n",
    "\n",
    "Map.add_time_slider(collection, vis_params, time_interval=2)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84",
   "metadata": {},
   "source": [
    "### Visualizing weather data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "collection = (\n",
    "    ee.ImageCollection(\"NOAA/GFS0P25\")\n",
    "    .filterDate(\"2018-12-22\", \"2018-12-23\")\n",
    "    .limit(24)\n",
    "    .select(\"temperature_2m_above_ground\")\n",
    ")\n",
    "\n",
    "vis_params = {\n",
    "    \"min\": -40.0,\n",
    "    \"max\": 35.0,\n",
    "    \"palette\": [\"blue\", \"purple\", \"cyan\", \"green\", \"yellow\", \"red\"],\n",
    "}\n",
    "\n",
    "labels = [str(n).zfill(2) + \":00\" for n in range(0, 24)]\n",
    "Map.add_time_slider(collection, vis_params, labels=labels, time_interval=1, opacity=0.8)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86",
   "metadata": {},
   "source": [
    "### Visualizing Sentinel-2 imagery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[37.75, -122.45], zoom=12)\n",
    "\n",
    "collection = (\n",
    "    ee.ImageCollection(\"COPERNICUS/S2_SR\")\n",
    "    .filterBounds(ee.Geometry.Point([-122.45, 37.75]))\n",
    "    .filterMetadata(\"CLOUDY_PIXEL_PERCENTAGE\", \"less_than\", 10)\n",
    ")\n",
    "\n",
    "vis_params = {\"min\": 0, \"max\": 4000, \"bands\": [\"B8\", \"B4\", \"B3\"]}\n",
    "\n",
    "Map.add_time_slider(collection, vis_params)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88",
   "metadata": {},
   "source": [
    "## Zonal statistics with Earth Engine\n",
    "\n",
    "### Zonal statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "# Add NASA SRTM\n",
    "dem = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "dem_vis = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 4000,\n",
    "    \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n",
    "}\n",
    "Map.addLayer(dem, dem_vis, \"SRTM DEM\")\n",
    "\n",
    "# Add 5-year Landsat TOA composite\n",
    "landsat = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n",
    "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n",
    "Map.addLayer(landsat, landsat_vis, \"Landsat\", False)\n",
    "\n",
    "# Add US Census States\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "style = {\"fillColor\": \"00000000\"}\n",
    "Map.addLayer(states.style(**style), {}, \"US States\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dem_stats = \"dem_stats.csv\"\n",
    "geemap.zonal_stats(\n",
    "    dem, states, out_dem_stats, stat_type=\"MEAN\", scale=1000, return_fc=False\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_landsat_stats = \"landsat_stats.csv\"\n",
    "geemap.zonal_stats(\n",
    "    landsat,\n",
    "    states,\n",
    "    out_landsat_stats,\n",
    "    stat_type=\"MEAN\",\n",
    "    scale=1000,\n",
    "    return_fc=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92",
   "metadata": {},
   "source": [
    "### Zonal statistics by group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "\n",
    "# Add NLCD data\n",
    "dataset = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\")\n",
    "landcover = dataset.select(\"landcover\")\n",
    "Map.addLayer(landcover, {}, \"NLCD 2019\")\n",
    "\n",
    "# Add US census states\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "style = {\"fillColor\": \"00000000\"}\n",
    "Map.addLayer(states.style(**style), {}, \"US States\")\n",
    "\n",
    "# Add NLCD legend\n",
    "Map.add_legend(title=\"NLCD Land Cover\", builtin_legend=\"NLCD\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlcd_stats = \"nlcd_stats.csv\"\n",
    "\n",
    "geemap.zonal_stats_by_group(\n",
    "    landcover,\n",
    "    states,\n",
    "    nlcd_stats,\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    decimal_places=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlcd_stats = \"nlcd_stats_pct.csv\"\n",
    "\n",
    "geemap.zonal_stats_by_group(\n",
    "    landcover,\n",
    "    states,\n",
    "    nlcd_stats,\n",
    "    stat_type=\"PERCENTAGE\",\n",
    "    denominator=1e6,\n",
    "    decimal_places=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96",
   "metadata": {},
   "source": [
    "### Zonal statistics with two images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[40, -100], zoom=4)\n",
    "dem = ee.Image(\"USGS/3DEP/10m\")\n",
    "vis = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n",
    "Map.addLayer(dem, vis, \"DEM\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98",
   "metadata": {},
   "outputs": [],
   "source": [
    "landcover = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select(\"landcover\")\n",
    "Map.addLayer(landcover, {}, \"NLCD 2019\")\n",
    "Map.add_legend(title=\"NLCD Land Cover Classification\", builtin_legend=\"NLCD\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99",
   "metadata": {},
   "outputs": [],
   "source": [
    "stats = geemap.image_stats_by_zone(dem, landcover, reducer=\"MEAN\")\n",
    "stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "100",
   "metadata": {},
   "outputs": [],
   "source": [
    "stats.to_csv(\"mean.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "101",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.image_stats_by_zone(dem, landcover, out_csv=\"std.csv\", reducer=\"STD\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "102",
   "metadata": {},
   "source": [
    "## Exporting images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "103",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n",
    "    [\"B5\", \"B4\", \"B3\"]\n",
    ")\n",
    "\n",
    "vis_params = {\"min\": 0, \"max\": 0.5, \"gamma\": [0.95, 1.1, 1]}\n",
    "\n",
    "Map.centerObject(image)\n",
    "Map.addLayer(image, vis_params, \"Landsat\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "104",
   "metadata": {},
   "outputs": [],
   "source": [
    "region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)\n",
    "fc = ee.FeatureCollection(region)\n",
    "style = {\"color\": \"ffff00ff\", \"fillColor\": \"00000000\"}\n",
    "Map.addLayer(fc.style(**style), {}, \"ROI\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "105",
   "metadata": {},
   "source": [
    "### To local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "106",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image(image, filename=\"landsat.tif\", scale=30, region=region)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107",
   "metadata": {},
   "source": [
    "### To Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "108",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_to_drive(\n",
    "    image, description=\"landsat\", folder=\"export\", region=region, scale=30\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "109",
   "metadata": {},
   "source": [
    "### To Asset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "110",
   "metadata": {},
   "outputs": [],
   "source": [
    "assetId = \"landsat_sfo\"\n",
    "geemap.ee_export_image_to_asset(\n",
    "    image, description=\"landsat\", assetId=assetId, region=region, scale=30\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "111",
   "metadata": {},
   "source": [
    "## Exporting image collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "112",
   "metadata": {},
   "outputs": [],
   "source": [
    "point = ee.Geometry.Point(-99.2222, 46.7816)\n",
    "collection = (\n",
    "    ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n",
    "    .filterBounds(point)\n",
    "    .filterDate(\"2008-01-01\", \"2018-01-01\")\n",
    "    .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "113",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection.aggregate_array(\"system:index\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "114",
   "metadata": {},
   "source": [
    "### To local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "115",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_collection(collection, out_dir=\".\", scale=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "116",
   "metadata": {},
   "source": [
    "### To Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "117",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_collection_to_drive(collection, folder=\"export\", scale=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "118",
   "metadata": {},
   "source": [
    "## Exporting feature collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "119",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "fc = states.filter(ee.Filter.eq(\"NAME\", \"Louisiana\"))\n",
    "Map.addLayer(fc, {}, \"Louisiana\")\n",
    "Map.centerObject(fc)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "120",
   "metadata": {},
   "source": [
    "### To local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "121",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_shp(fc, filename=\"louisiana.shp\", selectors=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "122",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_geojson(fc, filename=\"louisiana.geojson\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "123",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_csv(fc, filename=\"louisiana.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "124",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.ee_to_df(fc)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "125",
   "metadata": {},
   "source": [
    "### To Google Drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "126",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_vector_to_drive(\n",
    "    fc, description=\"louisiana\", fileFormat=\"SHP\", folder=\"export\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "127",
   "metadata": {},
   "source": [
    "## Creating timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = ee.ImageCollection(\"COPERNICUS/S2_HARMONIZED\").filterMetadata(\n",
    "    \"CLOUDY_PIXEL_PERCENTAGE\", \"less_than\", 10\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "129",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2016-01-01\"\n",
    "end_date = \"2022-12-31\"\n",
    "region = ee.Geometry.BBox(-122.5549, 37.6968, -122.3446, 37.8111)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "130",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = geemap.create_timeseries(\n",
    "    collection, start_date, end_date, region, frequency=\"year\", reducer=\"median\"\n",
    ")\n",
    "images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "131",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "vis_params = {\"min\": 0, \"max\": 4000, \"bands\": [\"B8\", \"B4\", \"B3\"]}\n",
    "labels = [str(y) for y in range(2016, 2023)]\n",
    "\n",
    "Map.addLayer(images, vis_params, \"Sentinel-2\", False)\n",
    "Map.add_time_slider(images, vis_params, time_interval=2, labels=labels)\n",
    "Map.centerObject(region)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "132",
   "metadata": {},
   "source": [
    "### Creating timelapse\n",
    "\n",
    "### Landsat timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "133",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "134",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-74.7222, -8.5867, -74.1596, -8.2824)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "135",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"landsat.gif\",\n",
    "    start_year=1984,\n",
    "    end_year=2022,\n",
    "    start_date=\"01-01\",\n",
    "    end_date=\"12-31\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=5,\n",
    "    title=\"Landsat Timelapse\",\n",
    "    progress_bar_color=\"blue\",\n",
    "    mp4=True,\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "136",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581)\n",
    "Map.addLayer(roi)\n",
    "Map.centerObject(roi)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "137",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"las_vegas.gif\",\n",
    "    start_year=1984,\n",
    "    end_year=2022,\n",
    "    bands=[\"NIR\", \"Red\", \"Green\"],\n",
    "    frames_per_second=5,\n",
    "    title=\"Las Vegas, NV\",\n",
    "    font_color=\"blue\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "138",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497)\n",
    "Map.addLayer(roi)\n",
    "Map.centerObject(roi)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "139",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.landsat_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"hong_kong.gif\",\n",
    "    start_year=1990,\n",
    "    end_year=2022,\n",
    "    start_date=\"01-01\",\n",
    "    end_date=\"12-31\",\n",
    "    bands=[\"SWIR1\", \"NIR\", \"Red\"],\n",
    "    frames_per_second=3,\n",
    "    title=\"Hong Kong\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "140",
   "metadata": {},
   "source": [
    "### MODIS timelapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "141",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "142",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = Map.user_roi\n",
    "if roi is None:\n",
    "    roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446)\n",
    "    Map.addLayer(roi)\n",
    "    Map.centerObject(roi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "143",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.modis_ndvi_timelapse(\n",
    "    roi,\n",
    "    out_gif=\"ndvi.gif\",\n",
    "    data=\"Terra\",\n",
    "    band=\"NDVI\",\n",
    "    start_date=\"2000-01-01\",\n",
    "    end_date=\"2022-12-31\",\n",
    "    frames_per_second=3,\n",
    "    title=\"MODIS NDVI Timelapse\",\n",
    "    overlay_data=\"countries\",\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "144",
   "metadata": {},
   "source": [
    "## Analyzing surface water dynamics\n",
    "\n",
    "### Surface water occurrence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "145",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.Image(\"JRC/GSW1_4/GlobalSurfaceWater\")\n",
    "dataset.bandNames()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "146",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "147",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = dataset.select([\"occurrence\"])\n",
    "region = Map.user_roi  # Draw a polygon on the map\n",
    "if region is None:\n",
    "    region = ee.Geometry.BBox(-99.957, 46.8947, -99.278, 47.1531)\n",
    "vis_params = {\"min\": 0.0, \"max\": 100.0, \"palette\": [\"ffffff\", \"ffbbbb\", \"0000ff\"]}\n",
    "Map.addLayer(image, vis_params, \"Occurrence\")\n",
    "Map.addLayer(region, {}, \"ROI\", True, 0.5)\n",
    "Map.centerObject(region)\n",
    "Map.add_colorbar(vis_params, label=\"Water occurrence (%)\", layer_name=\"Occurrence\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "148",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.image_histogram(\n",
    "    image,\n",
    "    region,\n",
    "    scale=30,\n",
    "    return_df=True,\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "149",
   "metadata": {},
   "outputs": [],
   "source": [
    "hist = geemap.image_histogram(\n",
    "    image,\n",
    "    region,\n",
    "    scale=30,\n",
    "    x_label=\"Water Occurrence (%)\",\n",
    "    y_label=\"Pixel Count\",\n",
    "    title=\"Surface Water Occurrence\",\n",
    "    layout_args={\"title\": dict(x=0.5)},\n",
    "    return_df=False,\n",
    ")\n",
    "hist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "150",
   "metadata": {},
   "outputs": [],
   "source": [
    "hist.update_layout(\n",
    "    autosize=False, width=800, height=400, margin=dict(l=30, r=20, b=10, t=50, pad=4)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "151",
   "metadata": {},
   "source": [
    "### Surface water monthly history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "152",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.ImageCollection(\"JRC/GSW1_4/MonthlyHistory\")\n",
    "dataset.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "153",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset.aggregate_array(\"system:index\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "154",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "155",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = dataset.filterDate(\"2020-08-01\", \"2020-09-01\").first()\n",
    "region = Map.user_roi  # Draw a polygon on the map\n",
    "if region is None:\n",
    "    region = ee.Geometry.BBox(-99.957, 46.8947, -99.278, 47.1531)\n",
    "vis_params = {\"min\": 0.0, \"max\": 2.0, \"palette\": [\"ffffff\", \"fffcb8\", \"0905ff\"]}\n",
    "\n",
    "Map.addLayer(image, vis_params, \"Water\")\n",
    "Map.addLayer(region, {}, \"ROI\", True, 0.5)\n",
    "Map.centerObject(region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region, scale=30, frequency=\"month\", denominator=1e4, y_label=\"Area (ha)\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "157",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"month\",\n",
    "    denominator=1e4,\n",
    "    y_label=\"Area (ha)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "158",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"year\",\n",
    "    reducer=\"mean\",\n",
    "    denominator=1e4,\n",
    "    y_label=\"Area (ha)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "159",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"year\",\n",
    "    reducer=\"max\",\n",
    "    denominator=1e4,\n",
    "    y_label=\"Area (ha)\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
